mini.parse();

function ConsumeLogHandler() {
};

ConsumeLogHandler.instance = new ConsumeLogHandler();

ConsumeLogHandler.isQueryBtnExpended = false;

//获得全局的消费日志数据表格对象
ConsumeLogHandler.consumeLogGrid = mini.get("consumeLogGrid");

//加载表格数据
ConsumeLogHandler.consumeLogGrid.load();

//清除日期数据
ConsumeLogHandler.conditionData = [
	{text: '选择清除的日期...', id: -1},
	{text: '一周之前', id: 0},
	{text: '一个月之前', id: 1},
	{text: '三个月之前', id: 2},
	{text: '半年之前', id: 3},
	{text: '一年之前', id: 4}
];

/**
 * 根据用户权限展示功能按钮
 */
ConsumeLogHandler.loadToolbar = function() {
	var array = new Array("consumelog_list_del", "consumelog_list_conditiondel", "consumelog_list_query", "consumelog_list_refresh");
	var userArr = MiniuiTools.isHasPurview(array);
	
	for ( var i = 0; i < userArr.length; i++) {
		mini.get(array[i]).setVisible(userArr[i]);
	}
};
ConsumeLogHandler.loadToolbar();

/**
 * 删除消费日志资源
 */
ConsumeLogHandler.delConsumeLog = function() {
	var rowsData = ConsumeLogHandler.consumeLogGrid.getSelecteds();
	
	if(rowsData.length > 0){
		mini.confirm("确认清除选择的" + rowsData.length + "条记录？" ,"系统提示",
			function(action, value){
				if (action == "ok") {
					var ids = "";
					
					for ( var i = 0; i < rowsData.length - 1; i++){
						ids += rowsData[i].lclId+ ",";
					}
					ids += rowsData[rowsData.length - 1].lclId;
					
					ConsumeLogHandler.consumeLogGrid.loading();
					
                    $.ajax({
                        url: __root + "/delConsumeLogByIds.do",
                        data: {
				        	lclIds: ids,
				        	lslUser: gbOperateUser,
				    		lslAddress: gbOperateIp
				        },
                        success: function (text) {
                        	MiniuiTools.miniInfo("系统提示","操作成功！", function(action){
				        		ConsumeLogHandler.consumeLogGrid.reload();
				        	});
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                        	MiniuiTools.miniError("系统提示", "操作失败！" + jqXHR.responseText);
                        },
                        complete: function(){
                        	ConsumeLogHandler.consumeLogGrid.unmask();
                        }
                    });	
                } 
			}
		)
	}else{
		MiniuiTools.miniWarning("系统提示","请选择要删除的资源！");
	}
};

/**
 * 弹出清除日志窗口
 */
ConsumeLogHandler.showDelLogWin = function(){
	miniWin.afreshNewWin(); 
	miniWin.setParams(336, 144, "选择清除日志条件");
	miniWin.setOption("bodyStyle","padding:0px;");
			
	var HTML = '<table style="width:100%;font-size:12px;padding-top:14px;" align="center" cellpadding="3" id="conditionTab"></table>';
     
	miniWin.getBodyEl().innerHTML = HTML;
	//只能先显示，再进行添加操作
	miniWin.show();
	
	var $tab = $('#conditionTab');
	var $tr1 = $('<tr></tr>');
	var $td1_1 = $('<td align="right" width="105">清除消费日志：</td>');
	var $td1_2 = $('<td></td>');
	var $td1_content = $('<input class="mini-combobox" id="dateCondition" textField="text" valueField="id" width="190"/>');
	var $tr2 = $('<tr></tr>');
	var $td2 = $('<td colspan="2" align="center" height="50px;"></td>');
	var $td2_content = $('<input class="mini-button" text="清除" iconCls="icon-save" plain="false" onclick="ConsumeLogHandler.delConsumeLogByCon"/>&nbsp;&nbsp;<input class="mini-button" text="关闭" iconCls="icon-cancel" plain="false" onclick="ConsumeLogHandler.closeMiniWin"/>');
	
	$td1_2.append($td1_content);
	$tr1.append($td1_1);
	$tr1.append($td1_2);	
	$td2.append($td2_content);
	$tr2.append($td2);	
	$tab.append($tr1);
	$tab.append($tr2);
	
	mini.parse();
	
	mini.get("dateCondition").setData(ConsumeLogHandler.conditionData);
	mini.get("dateCondition").setValue(-1);
}

/**
 * 执行条件清除日志
 */
ConsumeLogHandler.delConsumeLogByCon = function(){
	var condition = mini.get("dateCondition").getValue();
	var warningText = mini.get("dateCondition").getText();
	
	if(ConsumeLogHandler.consumeLogGrid.getData() == null 
			|| ConsumeLogHandler.consumeLogGrid.getData() == ""){
		MiniuiTools.miniWarning("系统提示","没有可删除的消费日志记录！");	
		return;
	}
	
	if(condition != null && condition != -1){
		mini.confirm("确认清除" + warningText + "的消费日志记录？" ,"系统提示",
			function(action, value){
				if (action == "ok") {
					var miniLoading = mini.loading();
					
                    $.ajax({
                        url: __root + "/delConsumeLogByCon.do",
                        data: {
				        	condition: condition,
				        	lslUser: gbOperateUser,
				    		lslAddress: gbOperateIp
				        },
                        success: function (text) {
                            MiniuiTools.miniInfo("系统提示","操作成功！", function(action){
				        		ConsumeLogHandler.consumeLogGrid.reload();
                            	miniWin.destroy();
				        	});
                            
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                        	MiniuiTools.miniError("系统提示", "操作失败！" + jqXHR.responseText);
                        },
                        complete: function(){
                        	mini.hideMessageBox(miniLoading);
                        }
                    });	
                } 
			}
		)
	}else{
		MiniuiTools.miniWarning("系统提示","请选择清除的日期条件！");
	}
}

/**
 * 展开或折叠查询消费日志面板
 */
ConsumeLogHandler.queryConsumeLog = function() {
	if (ConsumeLogHandler.isQueryBtnExpended) {
		ConsumeLogHandler.isQueryBtnExpended = false;
		mini.get("consumelog_list_query").setText("展开查询");
		mini.get("consumelog_list_query").setIconCls("icon-btn-expand");
	} else {
		ConsumeLogHandler.isQueryBtnExpended = true;
		mini.get("consumelog_list_query").setText("折叠查询");
		mini.get("consumelog_list_query").setIconCls("icon-btn-collapse");
	}
	
	$("#queryConsumeLogDiv").toggle(500);
    setTimeout(function(){mini.get("consumeLogPanel").doLayout();},600);
};

/**
 * 执行查询消费日志的方法
 */
ConsumeLogHandler.doQueryConsumeLog = function(){
	var bmbNick = mini.get("bmbNick").getValue();
	var lclEffect_begin = mini.get("lclEffect_begin").getValue() != null 
		? mini.get("lclEffect_begin").getValue().format("yyyy-MM-dd hh:mm:ss")
			: "";
	var lclEffect_end = mini.get("lclEffect_end").getValue() != null 
		? mini.get("lclEffect_end").getValue().format("yyyy-MM-dd hh:mm:ss")
			: ""; 

	ConsumeLogHandler.consumeLogGrid.load({
    	bmbNick: bmbNick, 
    	startTime: lclEffect_begin,
    	endTime: lclEffect_end
    });
};

/**
 * 刷新页面数据及布局
 */
ConsumeLogHandler.afresh = function(){
	ConsumeLogHandler.doQueryConsumeLog();
	mini.get("consumeLogPanel").doLayout();
}

/**
 * 销毁弹出窗体
 */
ConsumeLogHandler.closeMiniWin = function(){
	miniWin.destroy();
	// 弹出窗体页面被解析之后，mini-fit布局高度被重置为零，因此关闭窗体后需刷新界面
	ConsumeLogHandler.afresh();
}

/**
 * DrawCell 绘制单元格
 */
ConsumeLogHandler.consumeLogGrid.on("drawcell", function(e) {
	 var record = e.record;
     var column = e.column;
     var field = e.field;
     var value = e.value;
     
     if (column.field == "lclAmount") {
        if (e.value > 0) {
            e.cellHtml = "<span style='color:blue;'>+" + e.value + "</span>";                    
        } else {
            e.cellHtml = "<span style='color:red;'>" + e.value + "</span>";
        }
     }
});